﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
namespace EasyFramework.Web
{
    public class HtmlHelper
    {
        public HtmlHelper()
        {
        }
        //该方法是提取HTML中的图片地址，返回的多个图片地址用逗号分开。为什么要提取HTML中的图片地址？因为我要把网页或部分内容保存到本地，HTML中的图片是存放的图片地址，而不是图片的二进制代码，所以我必须把文件保存到本地，地址是必须知道的才能保存图片文件到本地，然后替换HTML中的图片地址为本地路径，才能实现在本地显示HTML图片在没有网络的情况下正常显示图片。
        public static string HtmImage(string htmlstring)
        {
            Regex regTag = new Regex(@"<[^>]*>");
            Regex regImg = new Regex(@"(?i)<img[^>]*?src=(['""]?)(?<src>[^'""\s>]+)\1[^>]*>");
            string result = regTag.Replace(htmlstring, delegate(Match m) { if (m.Value.ToLower().StartsWith("<img")) return regImg.Replace(m.Value, "《img src=\"${src}\"》《/img》"); return ""; });
            return result;
        }

        //该方法功能实现HTML转化成文本字符串，但是要保留图片的代码。这样做的目的是过滤一些HTML中的特殊HTML代码，但是还能正常显示图片。过滤HTML代码的好处，编写自己的CSS样式来美化自己的日志文件。不但开起来美观，有时候也能保护自己的眼睛，增加阅读兴趣。
        public static string HtmlToString(string Htmlstring, string websiteUrl, string PicPath)
        {
            Htmlstring = Htmlstring.Replace("<br>", "\r\n");
            Htmlstring = Htmlstring.Replace("<BR>", "\r\n");
            Htmlstring = Regex.Replace(Htmlstring, @"(<script)+[^<>]*>[^\0]*(<\/script>)+", "", RegexOptions.IgnoreCase);
            Htmlstring = Htmlstring.Replace("《", "\"");
            Htmlstring = Htmlstring.Replace("》", "\"");
            Htmlstring = HtmImage(Htmlstring);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring = Htmlstring.Replace("<", "");
            Htmlstring = Htmlstring.Replace(">", "");
            Htmlstring = Htmlstring.Replace("\r\n", "<br>");
            Htmlstring = Htmlstring.Replace("<script></script>", "");
            Htmlstring = Htmlstring.Replace("《", "<");
            Htmlstring = Htmlstring.Replace("》", "/>");
            Htmlstring = SavePic(Htmlstring, websiteUrl, PicPath);
            return Htmlstring;
        }
        //该方法实现把远程的图片保存到本地，以时间命名保存图片，并修改Html中的图片路径为本地相对路径，来完成本地显示日志内容。
        /// <summary>
        /// 保存远程图片到本地
        /// </summary>
        /// <param name="htmlstring">要修改的HTML字符串</param>
        /// <param name="websiteUrl">网站主域名</param>
        /// <param name="PicPath">保存图片的路径</param>
        /// <returns>返回处理过后的HTML字符串</returns>
        public static string SavePic(string htmlstring, string websiteUrl, string PicPath)
        {
            MatchCollection m;
            //提取字符串的图片
            m = Regex.Matches(htmlstring, "(<img).*?>");
            for (int i = 0; i < m.Count; i++)
            {
                string ostr = m[i].ToString();
                //提取图片的地址
                MatchCollection m2;
                m2 = Regex.Matches(ostr, "(src=)['|\"].*?['\"]");
                for (int j = 0; j < m2.Count; j++)
                {
                    string picurl = m2[j].ToString();
                    picurl = picurl.Replace("src=", "");
                    picurl = picurl.Replace("\"", "");
                    picurl = picurl.Replace("'", "");
                    string picname = DateTime.Now.ToString("yyyMMddHHmmss") + picurl.Substring(picurl.Length - 4, 4);
                    string newpic = "";
                    if (picurl.Substring(0, 4) != "http")
                    {
                        newpic = websiteUrl + "/" + picurl;
                    }
                    else
                    {
                        newpic = picurl;
                    }
                    try
                    {
                        WebClient wc = new WebClient();
                        wc.DownloadFile(newpic, PicPath + @"\" + picname);
                        htmlstring = htmlstring.Replace(newpic, @"pic/" + picname);
                    }
                    catch (Exception ex)
                    {
                    }

                }
            }
            return htmlstring;
        }
    }
}